﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VirtualPress.Tool.Framework;
using VirtualPress.Portal.Web.Models;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Infrastructure.HitMonitor;
using VirtualPress.Portal.WebLogic;
using System.Web.Mvc;

namespace VirtualPress.Portal.Web.Controllers
{
    public class DownloadController : Controller
    {
        public void Index(string token)
        {
            var model = EncodeByBase64.Decode(token).ToObject<DownloadModel>();
            Appendix attachment = null;
            switch (model.Type)
            {
                //下载自己的稿件 id: 附件id 
                case 1:
                    attachment = new Appendix().Find(model.Id.ToString());
                    if (attachment != null && attachment.Own == HttpContext.User.Identity.Name)
                    {
                        if (model.Journal > 0)
                        {
                            HitMonitor.Instance(HitName.Journal, model.Journal).Trigger((int)JournalGetAction.download);
                        }
                        FileDownLoad.DownLoadFile(
                            System.Web.HttpContext.Current.Request,
                            System.Web.HttpContext.Current.Response,
                            attachment.Path,
                            attachment.FileName + attachment.Path.Substring(attachment.Path.LastIndexOf(".")));
                        return;
                    }
                    break;
                //下载期刊的稿件
                case 9:
                    attachment = new Appendix().Find(model.Id.ToString());
                    if (attachment != null)
                    {
                        if (model.Journal > 0)
                        {
                            HitMonitor.Instance(HitName.Journal, model.Journal).Trigger((int)JournalGetAction.download);
                        }
                        HitMonitor.Instance(HitName.Article, model.Article).Trigger((int)ArticleGetAction.download);
                        FileDownLoad.DownLoadFile(
                            System.Web.HttpContext.Current.Request,
                            System.Web.HttpContext.Current.Response,
                            attachment.Path,
                            attachment.FileName + attachment.Path.Substring(attachment.Path.LastIndexOf(".")));
                        return;
                    }
                    break;
                //下载杂志期
                case 100:
                    var journalItem = new MagazineItem().Find(model.Id.ToString());
                    if (journalItem != null)
                    {
                        var journal = new Periodical().Find(journalItem.PeriodicalId.ToString());
                        if (model.Journal > 0)
                        {
                            HitMonitor.Instance(HitName.Journal, model.Journal).Trigger((int)JournalGetAction.download, new Random().Next(1, 30));
                        }
                        FileDownLoad.DownLoadFile(
                           System.Web.HttpContext.Current.Request,
                           System.Web.HttpContext.Current.Response,
                           journalItem.PagePath,
                           journal.Name + "第 " + journalItem.Manuscriptcode + " 期" + journalItem.PagePath.Substring(journalItem.PagePath.LastIndexOf(".")));
                        return;
                    }
                    break;
                //下载"相关下载"中的文件
                case 200:
                    var downloadFile = new Download().Find(model.Id.ToString());
                    if (downloadFile != null)
                    {
                        if (System.IO.File.Exists(Server.MapPath(downloadFile.Path)))
                        {
                            FileDownLoad.DownLoadFile(
                               System.Web.HttpContext.Current.Request,
                               System.Web.HttpContext.Current.Response,
                               downloadFile.Path,
                               downloadFile.Title + downloadFile.Path.Substring(downloadFile.Path.LastIndexOf(".")));
                        }
                    }
                    break;
            }
        }
    }
}